﻿using System.Linq;
using Kratos.Framework.ContentManagement.Models;
using Kratos.Framework.ContentManagement.Models.Order;
using NHibernate.Linq;

namespace Kratos.Framework.Repositories.NHibernateProvider
{
    public class NHibernateRepository<T> : IRepository<T>
        where T : class
    {
        protected readonly ISessionManager sessionManager;

        public NHibernateRepository(ISessionManager sessionManager)
        {
            this.sessionManager = sessionManager;
        }

        public T GetById(int id)
        {
            return sessionManager.Get().Get<T>(id);
        }

        public T LoadById(int id)
        {
            return sessionManager.Get().Load<T>(id);
        }

        public IQueryable<T> GetAll()
        {
            return sessionManager.Get().Query<T>();
        }

        public void SaveOrUpdate(T entity)
        {
            sessionManager.Get().SaveOrUpdate(entity);
        }

        public void DeleteOnSubmit(T entity)
        {
            sessionManager.Get().Delete(entity);
        }
    }
}
